package com.acc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.Admin;
import com.acc.entity.AdminRole;
import com.acc.entity.EasyUIDataGridJSON;
import com.acc.entity.Role;
import com.acc.service.AuthorityService;
import com.acc.service.impl.AuthorityServiceImpl;
import com.alibaba.fastjson.JSON;
/**
 * 
 * @author 朱春阳
 *
 */
public class AuthorityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AuthorityServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * 分页查询admin-role 的关系表
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		AuthorityService authority = new AuthorityServiceImpl();
		request.setCharacterEncoding("utf-8");
		//取出session中的管理员信息
		response.setCharacterEncoding("utf-8");
		
		Integer page = Integer.parseInt(request.getParameter("page"));
		Integer rows = Integer.parseInt(request.getParameter("rows"));
		Admin admin = (Admin) request.getSession().getAttribute("admin");
		//取出session中管理员对应的权限信息
		Role relation = (Role) request.getSession().getAttribute("role");
		if(admin != null) {
			String adminname = admin.getAdminname();
		}
		
		
		List<AdminRole> admins = authority.getAllAdminRole();
		List<AdminRole> pageAdmin = authority.getPageRole(page, rows);
		//总记录数目
		int count = admins.size();
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(count);
		responseObject.setRows(pageAdmin);
		String json = JSON.toJSONString(responseObject);
		response.setContentType("application/json");

		response.getWriter().append(json);
	}

}
